package com.jeesite.modules.warehouseshelves.web;

import java.util.ArrayList;
import java.util.List;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.OrgUtil;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.modules.warehouse.service.WarehouseService;
import com.jeesite.modules.warehousearea.service.WarehouseAreaService;
import com.jeesite.modules.warehouseshelves.entity.excel.WarehouseShelvesExport;
import com.jeesite.modules.warehouseshelves.entity.excel.WarehouseShelvesImport;
import com.jeesite.modules.warehouseshelves.entity.qo.WarehouseShelvesQO;
import com.jeesite.modules.warehouseshelves.entity.vo.WarehouseShelvesVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.warehouseshelves.entity.WarehouseShelves;
import com.jeesite.modules.warehouseshelves.service.WarehouseShelvesService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 货架档案Controller
 * @author 码脑洞
 * @version 2025-04-08
 */
@Controller
@RequestMapping(value = "${adminPath}/warehouseshelves/warehouseShelves")
public class WarehouseShelvesController extends BaseController {

	@Autowired
	private WarehouseShelvesService warehouseShelvesService;
	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private WarehouseAreaService warehouseAreaService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public WarehouseShelves get(Long autoId, boolean isNewRecord) {
		return warehouseShelvesService.get(autoId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("warehouseshelves:warehouseShelves:view")
	@RequestMapping(value = {"list", ""})
	public String list(WarehouseShelves warehouseShelves, Model model) {
		model.addAttribute("warehouseShelves", warehouseShelves);
		model.addAttribute("warehouseList", warehouseService.selectWarehouseList());
		model.addAttribute("warehouseAreaList", warehouseAreaService.selectWarehouseAreaList());
		return "modules/warehouseshelves/warehouseShelvesList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("warehouseshelves:warehouseShelves:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<WarehouseShelvesVO> listData(WarehouseShelvesQO warehouseShelvesQO, HttpServletRequest request, HttpServletResponse response) {
		warehouseShelvesQO.setPage(new Page<>(request, response));
		Page<WarehouseShelvesVO> page = warehouseShelvesService.findWarehouseShelvesPage(warehouseShelvesQO);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("warehouseshelves:warehouseShelves:view")
	@RequestMapping(value = "form")
	public String form(WarehouseShelves warehouseShelves, Model model) {
		if (OrgUtil.isValid()) {
			warehouseShelves.setOrgId(OrgUtil.getOrgId());
			warehouseShelves.setOrgCode(OrgUtil.getOrgCode());
			warehouseShelves.setOrgName(OrgUtil.getOrgName());
		}
		model.addAttribute("warehouseShelves", warehouseShelves);
		model.addAttribute("warehouseList", warehouseService.selectWarehouseList());
		model.addAttribute("warehouseAreaList", warehouseAreaService.selectWarehouseAreaList());
		return "modules/warehouseshelves/warehouseShelvesForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("warehouseshelves:warehouseShelves:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated WarehouseShelves warehouseShelves) {
		warehouseShelvesService.save(warehouseShelves);
		return renderResult(Global.TRUE, text("保存货架档案成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("warehouseshelves:warehouseShelves:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(WarehouseShelves warehouseShelves) {
		warehouseShelves.setStatus(WarehouseShelves.STATUS_DISABLE);
		warehouseShelvesService.updateStatus(warehouseShelves);
		return renderResult(Global.TRUE, text("停用货架档案成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("warehouseshelves:warehouseShelves:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(WarehouseShelves warehouseShelves) {
		warehouseShelves.setStatus(WarehouseShelves.STATUS_NORMAL);
		warehouseShelvesService.updateStatus(warehouseShelves);
		return renderResult(Global.TRUE, text("启用货架档案成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("warehouseshelves:warehouseShelves:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(WarehouseShelves warehouseShelves) {
		warehouseShelvesService.delete(warehouseShelves);
		return renderResult(Global.TRUE, text("删除货架档案成功！"));
	}


	@RequiresPermissions("warehouseshelves:warehouseShelves:edit")
	@RequestMapping(value = "checkCodeUnique")
	@ResponseBody
	public Boolean checkCodeUnique(String oldCode, String code) {
		return warehouseShelvesService.checkCodeUnique(oldCode, code);
	}

	@RequiresPermissions("warehouseshelves:warehouseShelves:view")
	@RequestMapping("exportData")
	public void exportData(WarehouseShelves warehouseShelves, HttpServletResponse response) {
		List<WarehouseShelves> list = warehouseShelvesService.findList(warehouseShelves);
		String fileName = "货架档案数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try (ExcelExport ee = new ExcelExport("货架档案", WarehouseShelvesExport.class)) {
			ee.setDataList(list).write(response, fileName);
		}
	}

	/**
	 * 下载导入数据模板
	 */
	@RequestMapping(value = "importTemplate")
	public void importTemplate(HttpServletResponse response) {
		String fileName = "货架档案-导入模板.xlsx";
		try (ExcelExport ee = new ExcelExport("货架档案", WarehouseShelvesImport.class, ExcelField.Type.IMPORT)) {
			ee.setDataList(new ArrayList<>()).write(response, fileName);
		}
	}

	/**
	 * 导入数据
	 */
	@ResponseBody
	@PostMapping(value = "importData")
	public String importData(MultipartFile file) throws Exception {
		return warehouseShelvesService.importData(file);
	}
}